package com.syqc.monitor.ui.carinfo

import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.LazyLoadSirFragment
import com.syqc.comlib.callback.EmptyCallback
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.loadStringImgNoneError
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragCarTmnPhotoBinding
import com.syqc.monitor.ui.newaddcar.PhotoViewPopup

/** @Author Jasper @Time 2021/2/27 @Desc  */
class CarTmnPhotosFragment :
    LazyLoadSirFragment<FragCarTmnPhotoBinding>(FragCarTmnPhotoBinding::inflate) {
    private val mViewModel by activityViewModels<CarInfoViewModel>()
    private val mVM by viewModels<CarTmnVM>()
    private val mAdapter by lazy { PhotosAdapter() }
    private val mPhotoView by lazy { PhotoViewPopup(requireActivity()) }

    //终端类型
    private var type = 1

    companion object {
        fun getInstance(): CarTmnPhotosFragment {
            val mFragment = CarTmnPhotosFragment()
            return mFragment
        }
    }


    override fun initView(view: View) {
        binding.rvTmnPhoto.layoutManager = LinearLayoutManager(requireContext())
        binding.rvTmnPhoto.adapter = mAdapter
        loadService.showCallback(EmptyCallback::class.java)

    }

    private var isMainHad = false
    private var isVideoHad = false
    private var isAdasHad = false

    override fun initData() {
        mVM.getCarInfo(mViewModel.carId)
        mVM.loadLive.observe(this) {
            isMainHad = !mVM.mainTmn.isNullOrEmpty()
            isVideoHad = !mVM.videoTmn.isNullOrEmpty()
            isAdasHad = !mVM.adasTmn.isNullOrEmpty()
            binding.rdbLocation.isVisible = isMainHad
            binding.rdbVideo.isVisible = isVideoHad
            binding.rdbAdas.isVisible = isAdasHad

            val isTmnShow = isMainHad || isVideoHad || isAdasHad
            binding.rdgTmn.isVisible = isTmnShow
            if (isTmnShow) loadService.showSuccess()
            binding.rvTmnPhoto.isVisible = isTmnShow
            if (!mVM.mainTmn.isNullOrEmpty()) {
                mAdapter.setList(mVM.mainTmn)
                type = 1
            } else if (!mVM.videoTmn.isNullOrEmpty()) {
                type = 2
                mAdapter.setList(mVM.videoTmn)
            } else if (!mVM.adasTmn.isNullOrEmpty()) {
                type = 3
                mAdapter.setList(mVM.adasTmn)
            }

            when {
                isMainHad -> {
                    binding.rdbLocation.isChecked = isMainHad
                }

                isVideoHad -> {
                    binding.rdbVideo.isChecked = isVideoHad
                }

                isAdasHad -> {
                    binding.rdbAdas.isChecked = isAdasHad
                }
            }
        }
    }

    override fun initEvent() {
        binding.rdbLocation.filterFast {
            if (type != 1) {
                type = 1
                mAdapter.setList(mVM.mainTmn)
            }
        }
        binding.rdbVideo.filterFast {
            if (type != 2) {
                type = 2
                mAdapter.setList(mVM.videoTmn)
            }
        }
        binding.rdbAdas.filterFast {
            if (type != 3) {
                type = 3
                mAdapter.setList(mVM.adasTmn)
            }
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            mPhotoView.show(mAdapter.data[position])
        }
    }
}

class PhotosAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_car_photo) {
    override fun convert(holder: BaseViewHolder, item: String) {
        item.run {
            holder.getView<ImageView>(R.id.iv_car_photo).loadStringImgNoneError(context, this)
        }
    }
}